<?php
/**
 * Created by PhpStorm.
 * User: zhongbaitong
 * Date: 2019/11/28
 * Time: 14:28
 */

namespace sys;


class Router
{
    // 单例
    use Singleton;
    // 控制器
    public $controller;
    // 方法
    public $action;

    /**
     * 构造函数
     * @access protected
     */
    protected function __construct()
    {
        $this->controller = config("app.default_controller");
        $this->action = config("app.default_action");

        $request = $_SERVER;
        // 路由处理
        if(isset($request["REQUEST_URI"]) && $request["REQUEST_URI"] != "/"){
            $str_path = str_replace("/index.php","",$request["REQUEST_URI"]);
            strchr($str_path,"?") && $str_path = substr($str_path,0,strpos($str_path,"?"));
            $arr_path = explode("/",trim($str_path,"/"));

            $this->controller = isset($arr_path[0]) && $arr_path[0]
                ? ucfirst($arr_path[0]) : "Index";
            $this->action = isset($arr_path[1]) ? strtolower($arr_path[1]) : "index";

            // 转换参数
            unset($arr_path[0],$arr_path[1]);
            for ($idx = 2; $idx < count($arr_path) + 2; $idx += 2){
                if(isset($arr_path[$idx+1])){
                    $_GET[$arr_path[$idx]] = $arr_path[$idx+1];
                }
            }
        }
    }


}